﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RVEAd.Business.Model;
using RVEAd.Business;

namespace RVEAd.Forms
{
    public partial class FormCatalogClienti : Form
    {
        private RveBL rveBl;

        public FormCatalogClienti()
        {
            InitializeComponent();
            dataGridClient.AutoGenerateColumns = false;

            this.Load += new EventHandler(FormCatalogClienti_Load);
            this.dataGridClient.CellDoubleClick += new DataGridViewCellEventHandler(dataGridClient_CellDoubleClick);
        }

        void dataGridClient_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            EditSelectedClient();            
        }

        void FormCatalogClienti_Load(object sender, EventArgs e)
        {
            rveBl = new RveBL();
            LoadClients();
        }


        /// <summary>
        /// Load all clients
        /// </summary>
        private void LoadClients()
        {
            var clients = rveBl.GetClients();
            dataGridClient.DataSource = clients;
        }

        /// <summary>
        /// Add form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdaugaClient_Click(object sender, EventArgs e)
        {
            Forms.FormAddEditClient addClientForm = new FormAddEditClient();

            addClientForm.IsEditMode = false;
            addClientForm.FormClosed += (oo, ss) =>
            {
                LoadClients();
            };

            addClientForm.ShowDialog();
        }

        /// <summary>
        /// Edit form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEditClient_Click(object sender, EventArgs e)
        {
            EditSelectedClient();
        }

        private void EditSelectedClient()
        {
            Forms.FormAddEditClient editClientForm = new FormAddEditClient();

            editClientForm.IsEditMode = true;
            editClientForm.Client = GetSelectedClient();
            editClientForm.FormClosed += (oo, ee) =>
            {
                LoadClients();
            };

            editClientForm.ShowDialog();
        }

        private Client GetSelectedClient()
        {
            if (dataGridClient.CurrentRow != null)
            {
                return (Client)dataGridClient.CurrentRow.DataBoundItem;
            }

            return null;
        }

        private void btnStergeClient_Click(object sender, EventArgs e)
        {
            var selectedClient = GetSelectedClient();
            if (selectedClient != null)
            {
                if (MessageBox.Show("Doresti sa sterg randul selectat ?", "RVE", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    RveBL rveBL = new RveBL();
                    rveBL.DeleteClient(selectedClient.ID);

                    LoadClients();
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
